//
//  OneImageFromTheLibraryInTheMuBan.m
//  PingTu
//
//  Created by Yangfan on 15/2/5.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "OneImageFromTheLibraryInTheMuBan.h"

@implementation OneImageFromTheLibraryInTheMuBan

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initImageView];
    }
    return self;
}

- (void)initImageView {
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor clearColor];
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.bounds, 0, 0)];
    [_contentView setBackgroundColor:[UIColor clearColor]];
    _contentView.delegate = self;
    _contentView.showsHorizontalScrollIndicator = NO;
    _contentView.showsVerticalScrollIndicator = NO;
    [_contentView setBounces:NO];
    // 支持放大缩小
    _contentView.userInteractionEnabled = YES;
    _contentView.delegate = self;
//    _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _contentView.maximumZoomScale=4.0;//最大倍率（默认倍率）
    _contentView.minimumZoomScale=1.0;//最小倍率（默认倍率）
    _contentView.decelerationRate=1.0;//减速倍率（默认倍率）
    [self addSubview:_contentView];
    
    self.imageview = [[UIImageView alloc] initWithFrame:self.bounds];
    _imageview.userInteractionEnabled = YES;
    [_imageview setBackgroundColor:[UIColor clearColor]];
    [_imageview setClipsToBounds:YES];
    _imageview.contentMode = UIViewContentModeScaleAspectFill;
    [_contentView addSubview:_imageview];
    
//    float minimumScale = self.frame.size.width / _imageview.frame.size.width;
//    [_contentView setMinimumZoomScale:minimumScale];
//    [_contentView setZoomScale:minimumScale];
    
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
}

- (void)setImageViewData:(UIImage *)imageData {
    _imageview.image= imageData;
    if (imageData == nil) {
        return;
    }
    
    CGRect rect  = CGRectZero;
    float imageWidth = _imageview.image.size.width;
    float imageHeight = _imageview.image.size.height;
    imageWidth = self.frame.size.width;
    imageHeight = imageWidth * imageHeight / _imageview.image.size.width;
    if (imageHeight < self.frame.size.height) {
        imageHeight = self.frame.size.height;
        imageWidth = imageHeight * _imageview.image.size.width / _imageview.image.size.height;
    }
    rect.size.width = imageWidth;
    rect.size.height = imageHeight;
    
    @synchronized(self){
        _imageview.frame = rect;
        
        [_contentView setContentSize:rect.size];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = [self.realCellArea CGPath];
        maskLayer.frame = _imageview.frame;
        self.layer.mask = maskLayer;
        
        CAShapeLayer *layerBorder = [CAShapeLayer layer];
        layerBorder.path = [self.realCellArea CGPath];
        layerBorder.fillColor = [[UIColor clearColor] CGColor];
        layerBorder.frame = _imageview.frame;
        layerBorder.strokeColor = [[UIColor redColor] CGColor];
        layerBorder.lineCap = kCALineCapRound;
        layerBorder.lineWidth = 0.0;  // 红色的边框宽度
        [self.layer addSublayer:layerBorder];
        
        [_contentView setZoomScale:0.2 animated:YES];
        [self setNeedsLayout];
    }
}

#pragma mark - UIScrollViewDelegate
#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageview;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
